<template>
  <div class="app-container">
  <!--站点数据-->
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="选择校区" prop="schoolId">
       <el-select v-model="queryParams.schoolId" placeholder="请选择" clearable filterable size="small" @keyup.enter.native="handleQuery" >
         <el-option
           v-for="dict in schoolOptions"
           :key="dict.value"
           :label="dict.name"
           :value="dict.id"
         />
       </el-select>
      </el-form-item>
      <el-form-item label="申请时间" prop="time">
        <el-date-picker
          v-model="queryParams.time"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="状态" clearable size="small" >
          <el-option :key="999" :label="'全部'" :value="null" />
          <el-option
            v-for="dict in runnerStatusOptions"
            :key="dict.key"
            :label="dict.value"
            :value="dict.key"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="跑腿员类型" prop="flg">
        <el-select v-model="queryParams.flg" placeholder="类型" clearable size="small" >
          <el-option :key="999" :label="'全部'" :value="null" />
          <el-option
            v-for="dict in runnerTypeOptions"
            :key="dict.key"
            :label="dict.value"
            :value="dict.key"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="跑腿员姓名" prop="username">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="跑腿员手机号" prop="mobile">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          :type="tableTab === 1 ? 'primary':''"
          size="mini"
          @click="changType(1)"
        >全部</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :type="tableTab === 2 ? 'primary':''"
          size="mini"
          @click="changType(2)"
        >待审核</el-button>
      </el-col>
    </el-row>


    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="50" align="center" /> -->
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="mobile" width="110px" />
      <el-table-column label="姓名" align="center" prop="username" />
      <el-table-column label="所属校区" align="center" prop="schoolName" />
      <el-table-column label="申请时间" align="center" prop="createTime" />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.stateName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="240">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row, '详情')"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详细 -->
    <el-dialog title="详细" :visible.sync="detailOpen" width="800px">
      <el-form ref="form" :model="form" label-width="120px" size="mini">
        <el-row>
          <el-col :span="3">
            <div style="width: 100px;height: 100px;border-radius: 50%;overflow: hidden;">
              <img style="height: 100%;" v-if="form.head" :src="form.head" >
              <img style="height: 100%;" v-else-if src="../../../assets/image/defaultHead.png" >
            </div>
          </el-col>
          <el-col :span="10">

            <el-col :span="20">
              <el-form-item label="跑腿员姓名：">{{ form.username }}</el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="跑腿员昵称：">{{ form.nickName }}</el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="手机号：">{{ form.mobile }}</el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="绑定微信：">{{ form.wechat }}</el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="年龄：">{{ form.age }}</el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="性别：">
                <div v-if="form.sex === '0'">男</div>
                <div v-else-if="form.sex === '1'">女</div>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="所属校区：">{{ form.schoolName }}</el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="申请时间：">{{ form.updateTime }}</el-form-item>
            </el-col>
          </el-col>
          <el-col :span="11">
            <el-col>
              <el-form-item label="身份证正面：">
                <el-col :span="12">
                  <img style="width: 200px;" :src="form.idCardFront" alt="身份证正面" />
                </el-col>
              </el-form-item>
              <el-form-item label="身份证背面：">
                <el-col :span="12">
                  <img style="width: 200px;" :src="form.idCardBack"  alt="身份证背面" />
                </el-col>
              </el-form-item>
              <el-form-item v-if="form.stuCard && form.typeName !== '平台'" label="学生证：">
                <el-col :span="12">
                  <img style="width: 200px;" :src="form.stuCard"  alt="学生证：" />
                </el-col>
              </el-form-item>
              </el-col>
          </el-col>
        </el-row>
        <div style="display: flex;align-items: center;justify-content: center;" v-if="form.checkFlg == 1">
          <el-button @click="handleCheck(0)">不通过</el-button>
          <el-button type="primary" @click="handleCheck(1)">通过审核</el-button>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { exportUser, } from "@/api/system/user";
import { runnerOption,listRunnerCheck,listRunner,listRunnerDetails,listRunnerCheckDetails,editRunner,checkRunner } from "@/api/pt/runner.js";
import { addSite } from "@/api/pt/site.js";
import { listSchool } from "@/api/pt/school.js";
import { getToken } from "@/utils/auth";

export default {
  name: "RunnerCheck",
  components: {},
  data() {
    return {
      // 列表数据简单e筛选
      tableTab: 1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      tableList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 已有校区所在城市数据
      ownCity: [],
      // 已有校区数据
      schoolOptions: [],
      // 跑腿员状态
      runnerStatusOptions: [],
      // 跑腿员类型
      runnerTypeOptions: [],
      // 表单参数
      form: {
      },
      selectId: '',
      //详情打开
      detailOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        status: undefined,
        schoolId: undefined,
        mobile: undefined
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "站点名称不能为空", trigger: "blur" }
        ],
        schoolId: [
          { required: true, message: "请选择校区", trigger: "blur" }
        ],
        address: [
          { required: true, message: "请输入详细地址", trigger: "blur" }
        ],
        time: [
          { required: true, message: "请选择时间", trigger: "blur" },
        ],
        email: [
          { required: true, message: "邮箱地址不能为空", trigger: "blur" },
          {
            type: "email",
            message: "'请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        phone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getList();

    listSchool({status:0,pageNum: 1,pageSize: 10000}).then(response => {
      let list = response.data.list
      let lArr = ''
      let listArr = []
      list.map(item=>{
        if(lArr.indexOf(item.city) == -1) {
          lArr += ',' + item.city
        }
      })
      lArr = lArr.split(',')
      lArr.map(item=>{
        listArr.push({
          label: item ? item : '全部',
          value: item
        })
      })
      this.schoolOptions = response.data.list
      this.ownCity = listArr
    });
    runnerOption().then(response => {
      this.runnerStatusOptions = response.data.state
      this.runnerTypeOptions = response.data.type
    })
  },
  methods: {
    /** 删除按钮操作 */
    handleCheck(state) {
      // let pa = JSON.parse(JSON.stringify(this.form))
      let pa = {
        "id": this.selectId,
        "msg": '',
        "result": state
      }
      if(state === 1){
        this.$confirm(`是否通过审核？`, "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return checkRunner(pa.id,pa);
        }).then(() => {
          this.getList();
          this.msgSuccess("修改成功");
          this.detailOpen = false
        }).catch(function() {});
      }else {
        this.$prompt('请输入不通过理由', "不通过", {
          confirmButtonText: "确定",
          cancelButtonText: "取消"
        }).then(({ value }) => {
          pa.msg = value
          return checkRunner(pa.id,pa);
        }).then(()=>{
          this.getList();
          this.msgSuccess("修改成功");
          this.detailOpen = false
        }).catch(() => {});
      }

    },
    //切换table
    changType(e){
      this.tableTab = e
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      if(this.tableTab === 2){
        listRunnerCheck(this.addDateRange2(this.queryParams, 'createTimeStart', 'createTimeEnd')).then(response => {
            this.tableList = response.data.list;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      }else {
        listRunner(this.addDateRange2(this.queryParams, 'createTimeStart', 'createTimeEnd')).then(response => {
            this.tableList = response.data.list;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
      this.form = {
        id: undefined,
        schoolId: undefined,
        name: undefined,
        city: undefined,
        status: undefined,
        address: undefined,
        siteDescribe: undefined,
        personInCharge: undefined,
        mobile: undefined,
        personInCharge2: undefined,
        mobile2: undefined,
        time: undefined,
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      console.log(row)
      this.selectId = row.id
      this.title = "修改";
      // row.time = [new Date(row.startTime),new Date(row.endTime)]
      // this.form = row
      if(this.tableList === 2){
        listRunnerCheckDetails(row.id).then(response => {
          this.form = response.data
          this.form.time = [new Date(response.data.startTime),new Date(response.data.endTime)]
          this.form.sex = JSON.stringify(this.form.sex)
          this.form.schoolId = JSON.stringify(this.form.schoolId)
        })
      }else{
        listRunnerDetails(row.id).then(response => {
          this.form = response.data
          this.form.time = [new Date(response.data.startTime),new Date(response.data.endTime)]
          this.form.sex = JSON.stringify(this.form.sex)
          this.form.schoolId = JSON.stringify(this.form.schoolId)
        })
      }
      if(type === '详情') {
        this.detailOpen = true;
      }else {
        this.open = true;
      }
    },
  }
};
</script>

<style type="text/css">
  .dian {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    display: inline-block;
    background: #999;
    margin-right: 6px;
  }
</style>
